package com.licheedev.glide_qrcode;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import com.bumptech.glide.signature.ObjectKey;
import java.nio.ByteBuffer;

public class QrCodeModuleLoader implements ModelLoader<QrCode, ByteBuffer> {

    @Nullable
    @Override
    public LoadData<ByteBuffer> buildLoadData(@NonNull QrCode qrCode, int width, int height,
        @NonNull Options options) {
        return new LoadData<ByteBuffer>(new ObjectKey(qrCode), new QrCodeFetcher(qrCode));
    }

    @Override
    public boolean handles(@NonNull QrCode qrCode) {
        return true;
    }

    public static class Factory implements ModelLoaderFactory<QrCode, ByteBuffer> {

        @NonNull
        @Override
        public ModelLoader<QrCode, ByteBuffer> build(
            @NonNull MultiModelLoaderFactory multiFactory) {
            return new QrCodeModuleLoader();
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }
}
